Skip to content

Fix tests which changed CWD without resetting - #54599

Merged
dsplaisted merged 1 commit into
dotnet:mainfrom
dsplaisted:fix-runparser-cwd-flake
Jun 5, 2026
Merged

dsplaisted merged 1 commit into
dotnet:mainfrom
dsplaisted:fix-runparser-cwd-flake

Conversation

@dsplaisted

Copy link
Copy Markdown
Member

Fix flaky AddReferenceHasDefaultArgumentSetToCurrentDirectory test

RunParserTests called Directory.SetCurrentDirectory without restoring it, leaking the process-wide current directory into later tests in the assembly. When it ran before AddReferenceHasDefaultArgumentSetToCurrentDirectory, that test's Directory.GetCurrentDirectory() no longer matched the argument's startup-cached default, causing a spurious failure.

Fix: restore the current directory after each test via IDisposable, and drop an unnecessary SetCurrentDirectory from the double-dash test (its project path is already absolute).

Seen in PR run for #54591

Copilot AI review requested due to automatic review settings June 4, 2026 19:25

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a source of test flakiness in dotnet.Tests by ensuring RunParserTests no longer leaks process-wide current working directory (CWD) changes into later tests, and by removing an unnecessary SetCurrentDirectory call in the -- argument parsing test.

Changes:

  • Add per-test CWD restoration to RunParserTests via IDisposable.
  • Remove redundant Directory.SetCurrentDirectory(...) usage where the project path is already absolute.
  • Keep the existing path-separator tests’ CWD behavior, but ensure it is reset after each test instance.

@Youssef1313 Youssef1313 left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

You can also consider using a custom xunit test framework implementation to assert this invariant for every test method in the whole assembly.

    public class MyTestFramework : XunitTestFramework
    {
        private sealed class MyDiscoverer : ITestFrameworkDiscoverer
        {
            private readonly ITestFrameworkDiscoverer _wrapped;

            public MyDiscoverer(ITestFrameworkDiscoverer wrapped)
                => _wrapped = wrapped;

            public ITestAssembly TestAssembly => _wrapped.TestAssembly;

            public ValueTask Find(Func<ITestCase, ValueTask<bool>> callback, ITestFrameworkDiscoveryOptions discoveryOptions, Type[] types = null, CancellationToken? cancellationToken = null)
                => _wrapped.Find(testCase => callback(new MyTestCase((IXunitTestCase)testCase)), discoveryOptions, types, cancellationToken);
        }

        private sealed class MyTestCase : IXunitTestCase, ISelfExecutingXunitTestCase
        {
            private readonly IXunitTestCase _wrapped;

            public MyTestCase(IXunitTestCase wrapped)
                => _wrapped = wrapped;

            public Type[] SkipExceptions => _wrapped.SkipExceptions;

            public string SkipReason => _wrapped.SkipReason;

            public Type SkipType => _wrapped.SkipType;

            public string SkipUnless => _wrapped.SkipUnless;

            public string SkipWhen => _wrapped.SkipWhen;

            public IXunitTestClass TestClass => _wrapped.TestClass;

            public int TestClassMetadataToken => _wrapped.TestClassMetadataToken;

            public string TestClassName => _wrapped.TestClassName;

            public string TestClassSimpleName => _wrapped.TestClassSimpleName;

            public IXunitTestCollection TestCollection => _wrapped.TestCollection;

            public IXunitTestMethod TestMethod => _wrapped.TestMethod;

            public int TestMethodMetadataToken => _wrapped.TestMethodMetadataToken;

            public string TestMethodName => _wrapped.TestMethodName;

            public string[] TestMethodParameterTypesVSTest => _wrapped.TestMethodParameterTypesVSTest;

            public string TestMethodReturnTypeVSTest => _wrapped.TestMethodReturnTypeVSTest;

            public int Timeout => _wrapped.Timeout;

            public bool Explicit => _wrapped.Explicit;

            public string SourceFilePath => _wrapped.SourceFilePath;

            public int? SourceLineNumber => _wrapped.SourceLineNumber;

            public string TestCaseDisplayName => _wrapped.TestCaseDisplayName;

            public string TestClassNamespace => _wrapped.TestClassNamespace;

            public int? TestMethodArity => _wrapped.TestMethodArity;

            public IReadOnlyDictionary<string, IReadOnlyCollection<string>> Traits => _wrapped.Traits;

            public string UniqueID => _wrapped.UniqueID;

            ITestClass ITestCase.TestClass => TestClass;

            ITestCollection ITestCase.TestCollection => TestCollection;

            ITestMethod ITestCase.TestMethod => TestMethod;

            int? ITestCaseMetadata.TestClassMetadataToken => TestClassMetadataToken;

            int? ITestCaseMetadata.TestMethodMetadataToken => TestMethodMetadataToken;

            public ValueTask<IReadOnlyCollection<IXunitTest>> CreateTests() => _wrapped.CreateTests();

            public void PostInvoke() => _wrapped.PostInvoke();

            public void PreInvoke() => _wrapped.PreInvoke();
            public ValueTask<RunSummary> Run(ExplicitOption explicitOption, IMessageBus messageBus, object[] constructorArguments, ExceptionAggregator aggregator, CancellationTokenSource cancellationTokenSource)
            {
                var previousDirectory = Directory.GetCurrentDirectory();
                try
                {
                    return await XunitRunnerHelper.RunXunitTestCase(
                        this,
                        messageBus,
                        cancellationTokenSource,
                        aggregator,
                        explicitOption,
                        constructorArguments);
                }
                finally
                {
                    // If any test modified current directory without restoring it, this assert will fail.
                    Assert.Equal(previousDirectory, Directory.GetCurrentDirectory());
                }
            }
        }

        protected override ITestFrameworkDiscoverer CreateDiscoverer(Assembly assembly) => new MyDiscoverer(base.CreateDiscoverer(assembly));

        protected override ITestFrameworkExecutor CreateExecutor(Assembly assembly) => base.CreateExecutor(assembly);
    }

MSBuild does that here

@dsplaisted
dsplaisted merged commit 9ced378 into dotnet:main Jun 5, 2026
23 of 26 checks passed
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 11.0-preview6 milestone Jun 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants